<?php
/**
 * Nordic Repository
 * 
 * LICENSE
 * 
 * The new BSD license is applied on this source-file. For further
 * information please visit http://license.nordic-dev.de/newbsd.txt
 * or send an email to andre.moelle@gmail.com.
 */

/**
 * This schema-class is used for encoding and decoding data of 
 * plain-text files.
 * 
 * @category   Nordic
 * @package    Nordic_Schema
 * @copyright  2007 Nordic Development
 * @license    http://license.nordic-dev.de/newbsd.txt (New-BSD license)
 * @author     Andre Moelle <andre.moelle@gmail.com>
 * @version    $Id: Txt.php 3 2007-07-08 09:21:42Z andre.moelle $
 */
class Nordic_Schema_Txt implements Nordic_Schema_Interface
{
	/**
	 * This method checks if the array has the right format.
	 * 
	 * The array must not have non-numeric keys or contain subarrays.
	 * If the delimiter (newline-characters) is in one element a
	 * notice is triggered.
	 * 
	 * @throws Invalid
	 * @access private
	 * @param Array $array Array to check
	 * @return void
	 */
	private function _checkArray ($array)
	{
		foreach($array as $key => $value)
		{
			if(!is_numeric($key))
			{
				throw new Nordic_Schema_Exception('Array has non-numeric keys.');
			}
			if(is_array($value))
			{
				throw new Nordic_Schema_Exception('Array contains subarrays.');
			}
			if(strpos($value, "\n") !== false)
			{
				trigger_error('Element of array contains newline-characters. ', E_USER_NOTICE);
			}
		}
	}
	
	/**
	 * @see Nordic_Schema_Interface::decode()
	 */
	public function decode ($array)
	{
		$this->_checkArray($array);
		
		return implode("\n", $array);
	}
	
	/**
	 * @see Nordic_Schema_Interface::encode()
	 */
	public function encode ($string)
	{
		return explode("\n", $string);
	}
}
?>